﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    public class HSVFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_HInterval = new DoubleInterval(6.0d, 18.0d);
        protected DoubleInterval m_SInterval = new DoubleInterval(double.MinValue, double.MaxValue);
        protected DoubleInterval m_VInterval = new DoubleInterval(double.MinValue, double.MaxValue);

        /// <summary>
        /// Default constructor
        /// </summary>
        public HSVFilter()
        {
        }

        /// <summary>
        /// Use this to specify intervals directly
        /// </summary>
        /// <param name="_h"></param>
        /// <param name="_s"></param>
        /// <param name="_b"></param>
        public HSVFilter(DoubleInterval _h, DoubleInterval _s, DoubleInterval _b)
        {
            m_HInterval = _h;
            m_SInterval = _s;
            m_VInterval = _b;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_hsbFilter"></param>
        internal HSVFilter(HSVFilter _hsbFilter)
        {
            this.m_HInterval = _hsbFilter.m_HInterval;
            this.m_SInterval = _hsbFilter.m_SInterval;
            this.m_VInterval = _hsbFilter.m_VInterval;
        }

        /// <summary>
        /// Gets or sets H
        /// </summary>
        public DoubleInterval H
        {
            get { return m_HInterval; }
            set { m_HInterval = value; }
        }

        /// <summary>
        /// Gets or sets H
        /// </summary>
        public DoubleInterval S
        {
            get { return m_SInterval; }
            set { m_SInterval = value; }
        }

        /// <summary>
        /// Gets or sets H
        /// </summary>
        public DoubleInterval V
        {
            get { return m_VInterval; }
            set { m_VInterval = value; }
        }


        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            HSV hsv = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    hsv = new HSV(bitmap.GetPixel(x,y));

                    if (m_HInterval.Contains(hsv.H)
                        && m_SInterval.Contains(hsv.S)
                        && m_VInterval.Contains(hsv.V))
                    {
                        bitmap.SetPixel(x, y, hsv.ToColor());

                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HSVFilter(this);
        }
    }
}
